package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.apps.common.constant.ProjectPlanStatusEnum;
import com.central.apps.service.IGanttService;
import com.central.apps.service.TaskStatisticsService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理进度填报业务逻辑
 *
 * @author lidachao
 * @date 2022/9/28
 */
@Component
@Slf4j
public class ScheduleFillHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private IGanttService ganttService;
    @Autowired
    private TaskStatisticsService taskStatisticsService;

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        if("complete".equals(flowNode.getId())) {
            // 查询进度申报信息
            List<Map<String, Object>> sfList = handlerService.findBySql("select proid, taskid, surpluspercent from gq_schedulefill where id = " + id);
            Map<String, Object> sfMap = sfList.get(0);
            Integer taskId = MapUtils.getInteger(sfMap,"taskid");
            Integer proId = MapUtils.getInteger(sfMap,"proid");
            float surpluspercent = MapUtils.getFloat(sfMap,"surpluspercent");

            // 查询计划信息
            Map<String, Object> taskMap = handlerService.findOneBySql("select * from gq_mytask where proid = " + proId + " and id = " + taskId);

            Map<String, Object> updMap = new HashMap<>();
            updMap.put("id", taskId);
            updMap.put("progress", 100 - surpluspercent);
            updMap.put("unfinishprocess", surpluspercent);

            Integer taskPlanId = MapUtils.getInteger(taskMap, "taskid");
            Map<String, Object> tpMap = new HashMap<>();
            tpMap.put("id", taskPlanId);
            tpMap.put("progress", 100 - surpluspercent);
            tpMap.put("unfinishprocess", surpluspercent);

            // 更新任务及计划进度
            handlerService.update("gq_mytask", updMap);
            handlerService.update("gq_scheduleplantask", tpMap);

            Integer mlId = MapUtils.getInteger(taskMap,"mlid");
            if (mlId != null && mlId > 0) {
                Map<String, Object> mlMap = new HashMap<>();
                mlMap.put("id", mlId);
                mlMap.put("progress", 100 - surpluspercent);
                mlMap.put("unfinishprocess", surpluspercent);

                handlerService.update("gq_pblmilestone", mlMap);
            }

            // 统计项目任务信息
            taskStatisticsService.taskStatistics(proId);

            // 更新项目任务进度
            ganttService.computeWeightedAverage(taskId.longValue(), "gq_mytask");
            // 更新项目计划进度
            ganttService.computeWeightedAverage(taskPlanId.longValue(), "gq_scheduleplantask");

            // 查询任务根节点进度
            Map<String, Object> map = handlerService.findOneBySql("select proid, progress from gq_scheduleplantask where parentId = 0 and proid = " + proId);
            Integer progress = MapUtils.getInteger(map, "progress");
            String status = "";
            if (progress == 100) {
                status = ProjectPlanStatusEnum.STATUS_DONE.getDesc();
            }

            StringBuilder updSql = new StringBuilder();
            updSql.append("update gq_proaccount t set proprocess = ").append(progress);

            if (StringUtils.isNotEmpty(status)) {
                updSql.append(", prostatus = '").append(status).append("'");
            }

            updSql.append(" where id = ").append(proId);

            // 更新项目进度
            handlerService.updateBySql(updSql.toString());
        }

        return null;
    }
}
